package com.ustb.springboot.controller;

import com.ustb.springboot.common.utils.R;
import com.ustb.springboot.entity.RoleEntity;
import com.ustb.springboot.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 角色
 *
 * @author pyn 2022/5/26
 */
@RestController
@RequestMapping("role")
public class RoleController {

    @Autowired
    private RoleService roleService;


    /**
     * 添加新角色
     * createRole
     *
     * @param roleEntity (roleName remark)
     *                   *
     */
    @RequestMapping("/createRole")
    public R createRole(@RequestBody RoleEntity roleEntity) {
        if (roleEntity.getRoleName() == null || roleEntity.getRemark() == null)
            return R.error("参数错误");
        return roleService.save(roleEntity) ? R.ok("添加角色成功！") : R.error("添加角色失败");
    }

    /**
     * 修改角色信息
     * updateRole
     *
     * @param roleEntity （新的角色名称 角色描述）
     **/
    @RequestMapping("/updateRole")
    public R updateRole(@RequestBody RoleEntity roleEntity) {
        return roleService.saveOrUpdate(roleEntity) ? R.ok("更新成功!") : R.error("更新失败!");
    }

    /**
     * ToDo
     * 根据传入角色id删除该角色
     *
     * @param roleId 角色id
     *               <p>
     *               删除对应角色的permission  dept 等信息
     */
    @RequestMapping("deleteRole")
    public R deleteRole(@RequestBody Integer roleId) {
        return roleService.deleteRoleById(roleId);
    }

}
